package com.example.naverbooksearch;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

import android.util.Log;
import android.widget.Toast;

import com.example.naverbooksearch.parser.InputStreamParserException;

public class NaverBookRequest extends NetworkRequest {

	String keyword;
	
	NaverBooks naverBooks;
	
	public NaverBookRequest(String keyword) {
		this.keyword = keyword;
	}
	
	@Override
	public void setRequestProperty(HttpURLConnection conn) {
		// TODO Auto-generated method stub
		super.setRequestProperty(conn);
		conn.setRequestProperty("Accept", "application/xml");
		//conn.setRequestProperty("appKey", newValue)
	}
	
	@Override
	public URL getRequestURL() {
		// TODO Auto-generated method stub
		String key = "b5c3a7c4a08f32f1c348f47f65da33f6";
		try {
			String urlString = "http://openapi.naver.com/search?key=" +
								key + "&query=" +
								URLEncoder.encode(keyword, "utf-8") + 
								"&display=10&start=1&target=book";
			Log.i("NaverBookRequest", urlString);
			URL url = new URL(urlString);
			return url;
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
							
		
		return null;
	}

	@Override
	public void parsing(InputStream is) throws ParsingException {
		// TODO Auto-generated method stub
		NaverBookSaxParser parser = new NaverBookSaxParser();
		try {
			parser.doParse(is);
			naverBooks = parser.getResult();
		} catch (InputStreamParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}

	@Override
	public NaverBooks getResult() {
		// TODO Auto-generated method stub
		return naverBooks;
	}

}
